home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / OVCMD.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  11KB  |  344 lines

  1. /*  025  31-May-87  ovcmd.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <process.h>
  8. #include "ov.h"
  9. #include "overr.h"
  10. #include "direct.h"
  11. #include "strmem.h"
  12.  
  13. static char cantexe[] = "can't execute!";
  14. static char str2big[] = "Expanded string is too large!";
  15.  
  16. extern WINDOW cw;
  17. extern FILE_ENT files[];
  18. extern PAG_ENT pag_tbl[];
  19. extern UDK_ENT udk_tbl[];
  20.  
  21. char *strrchr(), *strchr();
  22.  
  23. #ifdef LINT_ARGS
  24. int is_macro(char *, char *);
  25. char *expand_macro(char *, FILE_ENT *);
  26. char *cpyexp(char *, char *, char *);
  27. char *cpynoext(char *, char *, char *);
  28. #else
  29. int is_macro();
  30. char *expand_macro(), *cpyexp(), *cpynoext();
  31. #endif
  32.  
  33.  
  34. /******************************************************************************
  35.  **                         D O _ C M D                                      **
  36.  *****************************************************************************/
  37.  
  38. do_cmd() {             /* read and execute a single dos command */
  39.  
  40.    char *cmd;
  41.  
  42.    cmd = prompt(NULL,"Enter a single DOS command to execute",NULL,0,MAX_REPLY);
  43.    if (strlen(cmd) == 0)
  44.       return;
  45.  
  46.    run_cmd(cmd,TRUE,FALSE);     /* execute the command, pause, don't reload */
  47. }
  48.  
  49.  
  50. /******************************************************************************
  51.                              E X E C U T E
  52.  ******************************************************************************/
  53.  
  54. execute() {            /* execute the current file */
  55.  
  56.    char title[MAX_NAMELEN+10], *fn, *ext, *cmd, *fnbuf;
  57.  
  58.    /* make sure the file looks like its executable */
  59.  
  60.    if (files[cw.curidx].flags & DIR)
  61.       show_error(0,NO_EXE_DIR,1,"Can't execute a directory!");
  62.  
  63.    if ((ext = strrchr(fn = files[cw.curidx].name,'.')) == NULL ||
  64.        strlen(ext) < 4 || strstr(".EXE .COM .BAT",ext) == NULL) {
  65.       do_pagcmd(fn,ext);
  66.       return;
  67.    }
  68.  
  69.    fnbuf = fname(&files[cw.curidx]);   /* put cmd less ext into buffer */
  70.    *strrchr(fnbuf,'.') = '\0';
  71.    strcpy(title,"Execute ");
  72.    strcat(title,fn);
  73.  
  74.    cmd = prompt(title,"Add parameters to command, return to execute.",
  75.          fnbuf,strlen(fnbuf),MAX_REPLY);
  76.  
  77.    free(fnbuf);                        /* free fname() space */
  78.  
  79.    if (strlen(cmd) == 0)               /* user can cancel by clearing line */
  80.       return;
  81.  
  82.    run_cmd(cmd,TRUE,FALSE);            /* run command, pause, don't reload */
  83. }
  84.  
  85.  
  86. /******************************************************************************
  87.                            D O _ P A G C M D
  88.  *****************************************************************************/
  89.  
  90. static int
  91. do_pagcmd(fn,ext)      /* execute a Point-and-Go cmd for given extension */
  92. char *fn, *ext;
  93. {
  94.    int i;
  95.    register PAG_ENT *pp;
  96.  
  97.    if (ext)                    /* on input, is either NULL (file name has */
  98.       ext++;                   /* no extension) or it points to the '.'   */
  99.    else                        /* just prior to the extension. we don't   */
  100.       ext = "";                /* want to see the period or the NULL.     */
  101.  
  102.    /* try to find a matching extension in the extcmdtbl */
  103.  
  104.    for (i = 0, pp = pag_tbl; i < NUM_PAG; i++, pp++)
  105.  
  106.       if (pp->used && stricmp(ext,pp->ext) == 0) { /* matching ext? */
  107.          run_cmd(pp->cmd,pp->pause,pp->reload);    /* yes, run command */
  108.          return;
  109.       }
  110.  
  111.    /* didn't find a matching extension, tell user */
  112.  
  113.    show_error(0,NO_EXT,3,fn," does not have a known extension, ",cantexe);
  114. }
  115.  
  116.  
  117. /*****************************************************************************
  118.                               D O _ U D K
  119.  *****************************************************************************/
  120.  
  121. do_udk(udk_key)        /* execute UDK for given key */
  122. int udk_key;
  123. {
  124.    register UDK_ENT *up = &udk_tbl[udk_key - UDK_START];
  125.  
  126.    if (up->used)
  127.       run_cmd(up->cmd,up->pause,up->reload);
  128.    else
  129.       show_error(0,BAD_UDK,2,"That UDK is not defined, ",cantexe);
  130. }
  131.  
  132.  
  133. /******************************************************************************
  134.  **                          R U N _ C M D                                   **
  135.  *****************************************************************************/
  136.  
  137. run_cmd(cmd,pause,reload)      /* execute a dos command via command.com */
  138. char *cmd;
  139. int pause, reload;
  140. {
  141.    char *expcmd;
  142.    int has_tagged, i;
  143.    register FILE_ENT *fp;
  144.  
  145.    /* make sure there are tagged files if user want's tagged file names
  146.       as a command macro */
  147.  
  148.    if ((has_tagged = (is_macro(cmd,"t") || is_macro(cmd,"xt"))) && cw.num_tagged == 0)
  149.       show_error(0,NONE_TAGGED,2,"No files tagged, ",cantexe);
  150.  
  151.    clr_scr();                          /* home the cursor and clear screen */
  152.    showcursor();                       /* let user see the cursor */
  153.  
  154.    /* if there are tagged files, do the command once for each tagged file */
  155.  
  156.    if (has_tagged) {
  157.       for (i = 0, fp = files; i < cw.nfiles && !brkout(); i++, fp++)
  158.          if (fp->flags & TAGGED) {
  159.             expcmd = expand_macro(cmd,fp);
  160.             system(expcmd);
  161.             free(expcmd);
  162.          }
  163.  
  164.     } else {   /* otherwise just do the command once for current file */
  165.  
  166.        expcmd = expand_macro(cmd,&files[cw.curidx]);
  167.        system(expcmd);
  168.        free(expcmd);
  169.     }
  170.  
  171.    if (pause) {
  172.       putstr("\r\nPress any key to return to OVERVIEW.");
  173.       getchr();
  174.    }
  175.  
  176.    /* who knows what state things might be after a DOS command, restart
  177.       everything from scratch */
  178.  
  179.    reinit_tty();                       /* make sure the tty is setup right */
  180.    if (reload)
  181.       renew();                         /* read load files[], redo screen, etc */
  182.    else {
  183.       setup_file_scr();                /* display the static screen image */
  184.       update_header();                 /* update header info */
  185.       refresh_screen(0);               /* redisplay all file data */
  186.    }
  187. }
  188.  
  189.  
  190. /******************************************************************************
  191.  **                      S P A W N _ C L I                                   **
  192.  *****************************************************************************/
  193.  
  194. void
  195. spawn_cli() {          /* spawn a copy of the command interpeter */
  196.  
  197.    char comspec[MAX_PATHLEN+1], *ep, *getenv();
  198.  
  199.    clr_scr();                          /* home cursor, clear screen */
  200.    showcursor();                       /* show user the cursor */
  201.  
  202.    putstr("\r\nEnter the command 'EXIT' to return to OVERVIEW.\r\n");
  203.  
  204.    if (ep = getenv("COMSPEC"))         /* use COMSPEC env variable if defined */
  205.       strcpy(comspec,ep);
  206.    else
  207.       strcpy(comspec,"\COMMAND.COM");  /* otherwise hope its in the root dir */
  208.  
  209.    spawnl(P_WAIT,comspec,NULL);        /* run a copy of command.com */
  210.  
  211.    /* who knows what state things might be after going to DOS, restart
  212.       everything from scratch */
  213.  
  214.    reinit_tty();                       /* make sure tty is setup right */
  215.    setup_file_scr();                   /* display the static screen image */
  216.    update_header();                    /* update header info */
  217.    refresh_screen(0);                  /* redisplay all file data */
  218. }
  219.  
  220.  
  221. /******************************************************************************
  222.                                I S _ M A C R O
  223.  ******************************************************************************/
  224.  
  225. static int
  226. is_macro(str,type)     /* return TRUE if str contains macro of "type" */
  227. register char *str, *type;
  228. {
  229.    while (str = strchr(str,'$'))                       /* macros start with $ */
  230.       if (strnicmp(str+1,type,strlen(type)) == 0)      /* right type?         */
  231.          return(TRUE);                                 /*   found it          */
  232.       else
  233.          if (*(str+1) == '$')                  /* not a macro if $$   */
  234.             str += 2;
  235.          else
  236.             str++;                             /* skip over this $    */
  237.  
  238.    return(FALSE);                              /* not found */
  239. }
  240.  
  241.  
  242. /*****************************************************************************
  243.                           E X P A N D _ M A C R O
  244.  ****************************************************************************/
  245.  
  246. static char *
  247. expand_macro(str,fp)   /* expand any macros in str */
  248. char *str;
  249. FILE_ENT *fp;
  250. {
  251. #define MAXSTR 150
  252.    char expstr[MAXSTR+1];
  253.    register char *cp = str, *ep = expstr;
  254.  
  255.    /* make a quick exit if no macro in string */
  256.  
  257.    if (strchr(str,'$') == NULL)
  258.       return(Strdup(str));
  259.  
  260.    /* now expand the macros */
  261.  
  262.    while (*cp && ep - expstr <= MAXSTR) {
  263.  
  264.       if (*cp == '$') {                /* possible macro?   */
  265.  
  266.          if (*(cp+1) == '$') {         /* not a macro if $$ */
  267.             *ep++ = '$';
  268.             cp += 2;
  269.  
  270.          } else {                      /* maybe a macro     */
  271.  
  272.             switch (tolower(*(cp+1))) {
  273.                case 'c':               /* current file name */
  274.                   ep = cpyexp(ep,expstr,files[cw.curidx].name);
  275.                   break;
  276.                case 'd':               /* current drive     */
  277.                   *ep++ = *cw.dirbuf;
  278.                   break;
  279.                case 'p':               /* file's pathname   */
  280.                   ep = cpyexp(ep,expstr,fp->dirp);
  281.                   break;
  282.                case 't':               /* tagged file name  */
  283.                   ep = cpyexp(ep,expstr,fp->name);
  284.                   break;
  285.                case 'x':               /* file name without extension */
  286.                   if (tolower(*(cp+2)) == 'c') {
  287.                      ep = cpynoext(ep,expstr,files[cw.curidx].name);
  288.                      cp++;
  289.                   } else if (tolower(*(cp+2)) == 't') {
  290.                      ep = cpynoext(ep,expstr,fp->name);
  291.                      cp++;
  292.                   }
  293.                   break;
  294.                default:
  295.                   show_error(0,0,1,"Unknown macro in string!");
  296.                   return(Strdup(""));
  297.             }
  298.             cp += 2;                   /* skip over macro */
  299.          }
  300.  
  301.       } else                           /* normal char, just copy */
  302.          *ep++ = *cp++;
  303.    }
  304.  
  305.    if (*cp) {                                  /* should be at EOS */
  306.       show_error(0,0,1,str2big);
  307.       return(Strdup(""));
  308.    }
  309.  
  310.    *ep = '\0';                 /* terminate expanded string */
  311.  
  312.    return(Strdup(expstr));     /* return a copy to caller */
  313. }
  314.  
  315.  
  316. /******************************************************************************
  317.                           C P Y E X P / N O E X T
  318.  *****************************************************************************/
  319.  
  320. static char *
  321. cpyexp(ep,estr,mstr)   /* copy macro text to expanded string */
  322. char *ep, *estr, *mstr;
  323. {
  324.    if (ep - estr + strlen(mstr) <= MAXSTR) {   /* add it on if it will fit */
  325.       strcpy(ep,mstr);
  326.       return(ep+strlen(mstr));                 /* return EOS address */
  327.    } else
  328.       return(ep);
  329. }
  330.  
  331.  
  332. static char *
  333. cpynoext(ep,estr,mstr) /* copy macro text (filename) without extension */
  334. char *ep, *estr, *mstr;
  335. {
  336.    char *extp, fn[MAX_NAMELEN+1];
  337.  
  338.    strcpy(fn,mstr);                    /* cut off any extension of file name */
  339.    if (extp = strchr(fn,'.'))
  340.       *extp = '\0';
  341.  
  342.    return(cpyexp(ep,estr,fn));
  343. }
  344.